﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PlutoStudio
{
    public static class DelegateExtensions
    {
        public static void SafetyInvoke(this Delegate @event, params object[] parameters)
        {
            @event?.DynamicInvoke(parameters);
        }
        public static void SafetyInvoke(this EventHandler @event, object sender)
        {
            @event.SafetyInvoke(sender, EventArgs.Empty);
        }

        public static void SafetyInvoke(this EventHandler @event, object sender, EventArgs args)
        {
            @event?.Invoke(sender, args);
        }

        /// <summary>
        /// Raises given event safely with given arguments.
        /// </summary>
        /// <typeparam name="TEventArgs">Type of the <see cref="EventArgs"/></typeparam>
        /// <param name="event">The event handler</param>
        /// <param name="sender">Source of the event</param>
        /// <param name="e">Event argument</param>
        public static void SafetyInvoke<TEventArgs>(this EventHandler<TEventArgs> @event, object sender, TEventArgs e)
            where TEventArgs : EventArgs
        {
            @event?.Invoke(sender, e);
        }

    }
}
